//
//  BPChosenPicsViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/3/27.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPChosenPicsViewController.h"
#import "BPPreviewViewController.h"
#import <UIImageView+WebCache.h>

@interface BPChosenPicsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XLPhotoBrowserDatasource>

//图片的list
@property (strong, nonatomic) NSArray *picList;
//保存所有图片的imgV
@property (strong, nonatomic) NSMutableArray *picViews;

@end

@implementation BPChosenPicsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查看已选照片";
    self.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    [self getPicData];
}

- (NSMutableArray *)picViews {
    if (_picViews == nil) {
        _picViews = [NSMutableArray array];
    }
    return _picViews;
}

- (void)getPicData {
    [SVProgressHUD showWithStatus:@"读取中..."];
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    [mgr POST:[NSString stringWithFormat:@"%@?c=Order&&a=imgSelectByOrderID",requestURL]
   parameters:@{@"token" : user[@"token"],
                @"orderID" : self.orderID,
                @"page" : @"1",
                @"pagesize" : @"100"
                }
     progress:^(NSProgress * _Nonnull uploadProgress) {
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             
             [SVProgressHUD dismiss];
             NSLog(@"查看已选图片 %@", responseObject);
             self.picList = responseObject[@"data"][@"img"];
            
             //图片选择的collectionView
             CGFloat collectionPadding = 4;
             // 创建流水布局
             UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
             // 行间距
             flowLayout.minimumLineSpacing = collectionPadding;
             // 每个item 之间的间距
             flowLayout.minimumInteritemSpacing = collectionPadding;
             
             // 设置每个cell 的尺寸
             CGFloat cellWH = (screenW - 3 * collectionPadding) * 0.25;
             flowLayout.itemSize = CGSizeMake(cellWH, cellWH);
             
             // 设置滚动方向
             flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
             
             UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, screenW, screenH - 60 - 64) collectionViewLayout:flowLayout];
             collectionV.backgroundColor = [UIColor clearColor];
             [self.view addSubview:collectionV];
             [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"choosePicCell"];
             collectionV.delegate = self;
             collectionV.dataSource = self;
         }
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
     }];
}

#pragma mark - collection view datasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"choosePicCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UICollectionViewCell alloc]init];
    }
    
    UIImageView *picView = [[UIImageView alloc]init];
    [picView sd_setImageWithURL:[NSURL URLWithString:self.picList[indexPath.row][@"url"]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    cell.backgroundView = picView;
    [self.picViews addObject:picView];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picList.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


#pragma mark - collection view delegate
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:indexPath.row imageCount:self.picList.count datasource:self];
    [browser hideChooseBtn];
    browser.browserStyle = XLPhotoBrowserStyleShowSaveBtn;
    
}

#pragma mark    -   XLPhotoBrowserDatasource

- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *img = self.picViews[index];
    return img.image;
}

- (UIView *)photoBrowser:(XLPhotoBrowser *)browser sourceImageViewForIndex:(NSInteger)index
{
    return self.picViews[index];
}



@end
